﻿ko.Application.prototype.domains = ko.observableArray();
ko.Application.prototype.pages = ko.observableArray();

ko.Application.prototype.selecteddomain = ko.observable();
ko.Application.prototype.selectedpage = ko.observable();

ko.Application.prototype.addDomain = function ( data, event ) {
    var self = ko.controller;

    self.add( 'domain' );
};
ko.Application.prototype.addPage = function ( data, event ) {
    var self = ko.controller;

    if ( self.selecteddomain().domainId ) {
        self.add( 'page', { domainId: self.selecteddomain().domainId } );
    }
};
ko.Application.prototype.addChildPage = function ( data, event ) {
    setTimeout( function () {
        var self = ko.controller;

        if ( self.selectedpage().pageId ) {
            self.add( 'page', { pPageId: self.selectedpage().pageId } );
        }
    }, 0 );
}
ko.Application.prototype.onDomainClick = function ( data, event ) {
    var self = ko.controller;
    self.pages.splice( 0, self.pages().length );
    if ( data.domainId ) {
        self.load( 'page', { domainId: data.domainId }, function () {
            self.onPageClick( self.pages()[0] );
        } );
    };

    self.selecteddomain( data );
};
ko.Application.prototype.onPageClick = function ( data, event ) {
    var self = ko.controller;

    self.selectedpage( data );
};

ko.Application.prototype.init = function () {
    var self = this;
    self.load( 'domain', function () {
        self.onDomainClick( self.domains()[0] );
    } );
};

ko.domainModel = function ( domain ) {
    var self = this;
    self.__typeof = 'domain';

    self.domainId = domain.domainId;
    self.domainName = ko.observable( domain.domainName || '' );

    self._editing = ko.observable( domain._editing || false );
    self.event = {};
    self.event.edit = function ( domain ) {
        domain._editing( true );
    };
};

ko.pageModel = function ( page ) {
    var self = this;
    self.__typeof = 'page';

    self.domainId = page.domainId;
    self.pPageId = page.pPageId;
    self.pageId = page.pageId;
    self.title = ko.observable( page.title || '' );
    self.location = ko.observable( page.location || '' );
    self.icon = ko.observable( page.icon || '' );
    self.order = ko.observable( page.order || 0 );

    self.pages = ko.observableArray( ko.utils.arrayMap( page.pages, function ( page ) {
        return new ko.pageModel( page );
    } ) );

    self._editing = ko.observable( page._editing || false );
    self.event = {};
    self.event.edit = function ( page ) {
        page._editing( true );
    };
    self._expand = ko.observable( page._editing || false );
    self._level = ko.observable( page._level || 1 );
    self.event.expand = function ( page ) {
        var curExpand = page._expand();

        page.pages.splice( 0, page.pages().length );
        $.ajax( {
            url: $$.page.apisite + 'api/page/?' + $.param( {
                pPageId: page.pageId
            } ),
            type: 'GET'
        } ).then( function ( data ) {
            if ( $.isArray( data ) ) {
                data.forEach( function ( d ) {
                    d._level = page._level() + 1;
                    page.pages.push( new ko.pageModel( d ) );
                } );
            } else {
                data._level = page._level() + 1;
                page.pages.push( new ko.pageModel( data ) );
            }
        } );

        page._expand( !curExpand );
    };
};